<?php

#header('Content-type: application/json');

if (isset($_REQUEST['id']) and $_REQUEST['id']) {
	$categories = $db->fetchAll("SELECT * FROM blog_categories WHERE site_id=? AND parent_id=? ORDER BY sort", array($site['id'], $_REQUEST['id']));
	for($i=0;$i<count($categories);$i++) {
		$categories[$i]['children'] = $db->fetchOne("SELECT COUNT(*) FROM blog_categories WHERE parent_id=?", $categories[$i]['id']);
	}
	$json = array();
	foreach ($categories as $category) {
		$class = '';
		if ($category['children'] == 0) $class .= 'no-children';
		
		$json[] = array(
			'attributes' => array(
				'id'        => $category['id'], 
				'class'     => $class, 
				'rel'       => 'page', 
				'parent_id' => $category['parent_id']
			),
			'state' => 'closed',
			'data' => array('title' => $category['title'], 'icon' => "/admin/backend/resources/images/icons/folder.png")
		);
	}
	
} else {
	$json[] = array(
		'attributes' => array(
			'id'        => 1, 
			'class'     => '', 
			'rel'       => 'root', 
			'parent_id' => 0
		),
		'state' => 'closed',
		'data' => array('title' => 'Categories', 'icon' => "/admin/backend/resources/images/icons/feed.png")
	);
}

echo json_encode($json);

